﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CPhoneRC
{
    public partial class CompreForm : Form
    {
        public CompreForm()
        {
            InitializeComponent();
        }

        DataSet set = new DataSet();
        private void Comprehensive_form_Load(object sender, EventArgs e)
        {
            string str = "Data Source=SKY-20190902IUZ\\SHISONGPING;Initial Catalog=PhoneAdd;Integrated Security=True";
            SqlConnection conn = new SqlConnection(str);
            string sql = "select * from NUMRS";
            SqlDataAdapter dap = new SqlDataAdapter(sql, conn);
            dap.Fill(set);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = set.Tables[0];
            conn.Close();
            conn.Dispose();


            textBox3.Enabled = false;
            comboBox1.Enabled = false;
            textBox5.Enabled = false;
            textBox6.Enabled = false;
            textBox7.Enabled = false;
            textBox8.Enabled = false;
            textBox9.Enabled = false;
            textBox10.Enabled = false;
            textBox11.Enabled = false;
            textBox12.Enabled = false;
            button2.Enabled = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataView dv = set.Tables[0].DefaultView;
            dv.RowFilter = string.Format("num like '%{0}%' and ID like '%{1}%'", textBox1.Text,textBox2.Text);
            this.dataGridView1.DataSource = dv;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            textBox9.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            comboBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
            textBox10.Text= dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
            textBox5.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
            textBox11.Text = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
            textBox6.Text = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
            textBox12.Text = dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString();
            textBox7.Text = dataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString();
            label14.Text = dataGridView1.Rows[e.RowIndex].Cells[10].Value.ToString();
            textBox8.Text = dataGridView1.Rows[e.RowIndex].Cells[11].Value.ToString();
        }

        private void 修改数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox3.Enabled = true;
            comboBox1.Enabled = true;
            textBox5.Enabled = true;
            textBox6.Enabled = true;
            textBox7.Enabled = true;
            textBox8.Enabled = true;
            textBox9.Enabled = true;
            textBox10.Enabled = true;
            textBox11.Enabled = true;
            textBox12.Enabled = true;
            button2.Enabled = true;
            if (dataGridView1.SelectedRows.Count!=1)
            {
                MessageBox.Show("每次只能选择一行");
                return;
            }
            
        }

        DataSet DataSet = new DataSet();
        private void button2_Click(object sender, EventArgs e)
        {

            if (textBox3.Text == ""||textBox9.Text=="")
            {
                MessageBox.Show("姓名或身份证号码不能为空！");
                return;
            }
            string sql = string.Format("update NUMRS set num = '{0}',sex = '{1}',region='{2}',number='{3}',email='{4}',remakes='{5}',ID='{6}',age='{7}',Company='{8}',QQ='{9}'where goodid ='{10}' ; ", textBox3.Text, comboBox1.Text, textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, textBox9.Text, textBox10.Text, textBox11.Text, textBox12.Text,dataGridView1.SelectedCells[0].Value.ToString());
            bool result = DBHelper.ExecuteNonQuery(sql);
            if (result == true)
            {
                MessageBox.Show("修改成功！");
            }
            else
            {
                MessageBox.Show("修改失败！");
            }

            string str = "Data Source=SKY-20190902IUZ\\SHISONGPING;Initial Catalog=PhoneAdd;Integrated Security=True";
            SqlConnection conn = new SqlConnection(str);
            string ssql = "select * from NUMRS ";
            SqlDataAdapter adapter = new SqlDataAdapter(ssql, conn);
            adapter.Fill(DataSet);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = DataSet.Tables[0];
            conn.Close();
            conn.Dispose();
        }

        private void 销户操作ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count!=1)
            {
                MessageBox.Show("每次只能选择一行");
                return;
            }
            else
            {
                string sql = "delete from NUMRS where goodid="+dataGridView1.SelectedCells[0].Value;
                bool result = DBHelper.ExecuteNonQuery(sql);
                if (result==true)
                {
                    MessageBox.Show("销户成功！");
                }
            }


            //成功查询展示更新后的数据
            string str = "Data Source=SKY-20190902IUZ\\SHISONGPING;Initial Catalog=PhoneAdd;Integrated Security=True";
            SqlConnection conn = new SqlConnection(str);
            string ssql = "select * from NUMRS ";
            SqlDataAdapter adapter = new SqlDataAdapter(ssql, conn);
            adapter.Fill(DataSet);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = DataSet.Tables[0];
            conn.Close();
            conn.Dispose();
        }
    }
}
